﻿using PagedList;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YuFeng.Models;
using YuFeng.Repositories;
using YuFeng.Repositories.Product;

namespace YuFeng.Services.Product
{
    public interface IProductionService : IBaseService<Models.Product.Product>
    {

        IPagedList<Models.Product.Product> GetPageProducts(string name, int? type, bool? isdelete, int page = 1, int size = 10);
        Models.Product.Product GetProductByID(string id);

        Models.Product.Product GetApplication();
        List<Models.Product.Product> GetIndexProducts();
    }
    public class ProductionService : BaseService<Models.Product.Product>, IProductionService
    {
        private readonly IProductionRepository _productionRepository;
        private readonly IUnitOfWork _unitOfWork;
        public ProductionService(IProductionRepository productionRepository, IUnitOfWork unitOfWork)
            : base(productionRepository, unitOfWork)
        {
            this._productionRepository = productionRepository;
            this._unitOfWork = unitOfWork;
        }
        public IPagedList<Models.Product.Product> GetPageProducts(string name, int? type, bool? isdelete, int page = 1, int size = 10)
        {
            var query = _productionRepository.GetAll();

            if (!string.IsNullOrWhiteSpace(name))
            {
                query = query.Where(e => e.Name.IndexOf(name) > -1);
            }
            if (type.HasValue)
            {
                query = query.Where(e => e.Type == type.Value);
            }
            if (isdelete.HasValue)
            {
                query = query.Where(e => e.IsDeleted == isdelete.Value);
            }

            return query.GetPage(o => o.CreatedTime, page, size, true);
        }
        public Models.Product.Product GetProductByID(string id)
        {
            var query = _productionRepository.GetById(id);
            return query;
        }
        public Models.Product.Product GetApplication()
        {
            var query = _productionRepository.Get(o => o.Type == (int)AppEnum.ProductionType.IndustryApplication && o.IsDeleted == false) ?? new Models.Product.Product();
            return query;
        }

        public List<Models.Product.Product> GetIndexProducts()
        {
            var query = _productionRepository.GetAll().Where(o => o.IsIndex == true && o.Type == (int)AppEnum.ProductionType.ProdoctCenter).ToList();
            return query;
        }
    }
}
